package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.service.SysUserService;
import com.b511.swpu.cementbackend.dto.input.SysUserInputDto;
import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.repository.SysUserRepository;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Api(value = "管理员", tags = {"管理员"})
@RestController
@RequestMapping("/admin")
public class AdminController {

    private final SysUserService sysUserService;

    private final SysUserRepository sysUserRepository;

    public AdminController(SysUserService sysUserService, SysUserRepository sysUserRepository) {
        this.sysUserService = sysUserService;
        this.sysUserRepository = sysUserRepository;
    }

    @PutMapping("/user/{userId}")
    public R updateUser(@PathVariable String userId, @RequestBody SysUserInputDto sysUserInputDto) {
        return sysUserService.updateUser(userId, sysUserInputDto);
    }

    @PostMapping("/user")
    @Transactional
    public R addUser(@RequestBody SysUserInputDto sysUserInputDto) {
        SysUser user=new SysUser();
        BeanUtils.copyProperties(sysUserInputDto,user);
        sysUserRepository.save(user);
        return R.ok();
    }
}
